package com.dreamwhale.machinery.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.machinery.config.DataResult;
import com.dreamwhale.machinery.dao.*;
import com.dreamwhale.machinery.entity.*;
import com.dreamwhale.machinery.goods.entity.*;
import com.dreamwhale.machinery.goods.mapper.*;
import com.dreamwhale.machinery.goods.service.ProjectSeekService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dreamwhale.machinery.logistics.entity.TransportCompanyInfo;
import com.dreamwhale.machinery.logistics.entity.TransportInfo;
import com.dreamwhale.machinery.logistics.mapper.TransportCompanyInfoMapper;
import com.dreamwhale.machinery.logistics.mapper.TransportInfoMapper;
import com.dreamwhale.machinery.user.entity.UserShop;
import com.dreamwhale.machinery.user.mapper.UserShopMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 找工程 服务实现类
 * </p>
 *
 * @author Author
 * @since 2020-05-28
 */
@Service
public class ProjectSeekServiceImpl extends ServiceImpl<ProjectSeekMapper, ProjectSeek> implements ProjectSeekService {
    @Resource
    private SlideshowMapper slideshowMapper;
    @Resource
    private NoticeMapper noticeMapper;


    @Resource
    private DriverInfoMapper driverInfoMapper;
    @Resource
    private DriverWorkInfoMapper driverWorkInfoMapper;
    @Resource
    private MechanicInfoMapper mechanicInfoMapper;
    @Resource
    private ProjectInfoMapper projectInfoMapper;
    @Resource
    private ProjectSeekMapper projectSeekMapper;
    @Resource
    private TransportCompanyInfoMapper transportCompanyInfoMapper;
    @Resource
    private UserShopRecommendMapper userShopRecommendMapper;
    @Resource
    private UserShopMapper userShopMapper;
    @Resource
    private YxStoreCategoryMapper yxStoreCategoryMapper;
    @Resource
    private YxStoreProductMapper yxStoreProductMapper;
    @Resource
    private UserShopCollectMapper userShopCollectMapper;
    @Resource
    private TransportInfoMapper transportInfoMapper;
    @Resource
    private LeaseMapper leaseMapper;
    @Resource
    private MachineBuyMapper machineBuyMapper;
    @Resource
    private MachineSellMapper machineSellMapper;

    @Resource
    private YxSystemGroupDataMapper yxSystemGroupDataMapper;

    @Override
    public DataResult getIndex(String city) {
        HashMap<String, Object> resultMap = new HashMap<>();
        List<SlideShow> slideShows = slideshowMapper.selectList(new QueryWrapper<>());
        List<String> slideShow = slideShows.stream().map(SlideShow::getSlideshowPicture).collect(Collectors.toList());
        List<Notice> notices = noticeMapper.selectList(new QueryWrapper<>());
        List<String> notice = notices.stream().map(Notice::getNoticeDetils).collect(Collectors.toList());
        DriverInfo driverInfoCountNumber = driverInfoMapper.getHot();
        DriverWorkInfo driverWorkInfoCountNumber = driverWorkInfoMapper.getHot();
        ProjectInfo projectInfoCountNumber = projectInfoMapper.getHot();
        ProjectSeek projectSeekCountNumber = projectSeekMapper.getHot();
        TransportCompanyInfo transportCompanyInfoCountNumber = transportCompanyInfoMapper.getHot();
        TransportInfo transportInfo = transportInfoMapper.getHot();
        Lease lease = leaseMapper.getHot();
        MachineSell machineSell = machineSellMapper.getHot();
        MachineBuy machineBuy = machineBuyMapper.getHot();
        List<UserShopRecommend> userShopRecommends = userShopRecommendMapper.selectList(new QueryWrapper<>());
        List<Integer> userShopRecommendList = userShopRecommends.stream().map(UserShopRecommend::getShopId).collect(Collectors.toList());

        QueryWrapper<UserShop> userShopQueryWrapper = new QueryWrapper<>();
        userShopQueryWrapper.eq("status", 1);
        if (StringUtils.isNotBlank(city)) {
            userShopQueryWrapper.eq("city", city);
        }
        userShopQueryWrapper.in("id", userShopRecommendList);
        List<UserShop> userShopRecommendListInfo = userShopMapper.selectList(userShopQueryWrapper);
        List<YxStoreCategory> pid = yxStoreCategoryMapper.selectList(new QueryWrapper<YxStoreCategory>().eq("pid", 0));
        List<Integer> collect = pid.stream().map(YxStoreCategory::getId).collect(Collectors.toList());
        List<YxStoreCategory> partsPid = yxStoreCategoryMapper.selectList(new QueryWrapper<YxStoreCategory>().in("pid", collect));
        List<YxStoreProduct> yxStoreProducts = yxStoreProductMapper.selectList(new QueryWrapper<YxStoreProduct>().eq("is_hot", 1).eq("is_del", 0).eq("is_show", 1).orderByDesc("sales"));
        QueryWrapper<YxSystemGroupData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_name", "yshop_home_banner").eq("del_flag", 0);
        List<YxSystemGroupData> yxSystemGroupData = yxSystemGroupDataMapper.selectList(queryWrapper);

        ArrayList<Object> objects = new ArrayList<>();
        objects.add(driverInfoCountNumber);
        objects.add(driverWorkInfoCountNumber);
        objects.add(projectInfoCountNumber);
        objects.add(projectSeekCountNumber);
        objects.add(lease);
        objects.add(machineSell);
        objects.add(machineBuy);
        objects.add(transportInfo);
        objects.add(transportCompanyInfoCountNumber);
        resultMap.put("slideShows", slideShow);
        resultMap.put("notices", notice);
        resultMap.put("HotMessage", objects);
        resultMap.put("RecommendShopList", userShopRecommendListInfo);
        resultMap.put("bannerList", yxSystemGroupData);
        if (yxStoreProducts.size() > 5)
            resultMap.put("HotParts", yxStoreProducts.subList(0, 5));
        else
            resultMap.put("HotParts", yxStoreProducts.subList(0, yxStoreProducts.size()));
        return new DataResult(200, "查询成功", resultMap);
    }

    @Override
    public DataResult queryConditionCommercialTenant(String str, Integer brandId, String city, Integer salesVolume
            , Integer page, Integer limit) {
        try {
            QueryWrapper<YxStoreProduct> yxStoreProductQueryWrapper = new QueryWrapper<>();
            yxStoreProductQueryWrapper.eq("is_show", 1).eq("is_del", 0);
            if (brandId != null) yxStoreProductQueryWrapper.eq("brand_id", brandId);
            if (salesVolume != null) {
                if (salesVolume == 1) yxStoreProductQueryWrapper.orderByDesc("sales");
                else yxStoreProductQueryWrapper.orderByAsc("sales");
            }
            PageHelper.startPage(page, limit, true);
            List<YxStoreProduct> yxStoreProducts = yxStoreProductMapper.selectList(yxStoreProductQueryWrapper);
            PageInfo<YxStoreProduct> pages = new PageInfo<>();
            List<Integer> collect = yxStoreProducts.stream().map(YxStoreProduct::getMerId).distinct().collect(Collectors.toList());
            ArrayList<Object> objects = new ArrayList<>();
            for (Integer integer : collect) {
                QueryWrapper<UserShop> userShopWrapper = new QueryWrapper<>();
                if (StringUtils.isNotBlank(city)) userShopWrapper.eq("city_code", city);
                if (StringUtils.isNotBlank(str)) userShopWrapper.like("merchant_name", str);
                userShopWrapper.eq("id", integer).eq("status", 1);
                UserShop userShop = userShopMapper.selectOne(userShopWrapper);
                if (userShop != null) objects.add(userShop);
            }
            return new DataResult(200, "查询成功", PageInfo.of(objects));
        } catch (Exception e) {
            return new DataResult(300, "查询失败", null);
        }
    }

    @Override
    public int insertProjectSeeks(ProjectSeek projectSeek) {
        projectSeek.setCreateTime(LocalDateTime.now());
        return projectSeekMapper.insert(projectSeek);
    }

    @Override
    public List<ProjectSeek> selectProjectSeekMap(Integer userId) {
        QueryWrapper<ProjectSeek> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("del_flag", 0);

        return projectSeekMapper.selectList(queryWrapper);
    }

    @Override
    public List<ProjectSeek> selectProjectSeeks(Integer infoId) {
        QueryWrapper<ProjectSeek> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0).eq("id", infoId);

        return projectSeekMapper.selectList(queryWrapper);
    }

    @Override
    public ProjectSeek selectProjectByTypeId(Integer infoId) {
        return projectSeekMapper.selectProjectByTypeId(infoId);
    }

    @Override
    public DataResult queryMerchantDetails(Integer merchantId, Integer userId) {
        UserShop shopInfo = userShopMapper.selectOne(new QueryWrapper<UserShop>().eq("id", merchantId));
        QueryWrapper<YxStoreProduct> yxStoreProductQueryWrapper = new QueryWrapper<>();

        QueryWrapper<UserShopCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id", merchantId).eq("uid", userId);
        List<UserShopCollect> list = userShopCollectMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            shopInfo.setCollect(1);
        } else {
            shopInfo.setCollect(0);
        }
        yxStoreProductQueryWrapper.eq("mer_id", merchantId).eq("is_del", 0).eq("is_show", 1);
        List<YxStoreProduct> yxStoreProductList = yxStoreProductMapper.selectList(yxStoreProductQueryWrapper);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("shopInfo", shopInfo);
        resultMap.put("yxStoreProductList", yxStoreProductList);
        return new DataResult(200, "查询成功", resultMap);
    }

    @Override
    public List<ProjectSeek> selectProjectSeekHome(Integer type, Integer types, Integer brandId, Integer typeId, String title) {

        QueryWrapper<ProjectSeek> queryWrapper = new QueryWrapper<>();
        if (type != null) {
            queryWrapper.orderByDesc("count_number");
        }

        if (types != null) {
            queryWrapper.orderByDesc("create_time");
        }

        if (brandId != null) {
            queryWrapper.eq("brand_id", brandId);
        }
        if (typeId != null) {
            queryWrapper.eq("cate_id", typeId);
        }
        if (title != null) {
            queryWrapper.like("title", title);
        }
        queryWrapper.eq("del_flag", 0);
        return projectSeekMapper.selectList(queryWrapper);
    }
}
